package zdream.control.test.world.terrain;

import org.junit.Test;
import zdream.control.input.SimpleWorldInput;
import zdream.control.input.WorldInput;
import zdream.control.level.LevelManagerForTest;
import zdream.control.profile.mm.MegaManProfile;
import zdream.control.region.Region;
import zdream.control.region.SimpleRegionBuilder;
import zdream.control.world.LevelWorld;
import zdream.control.world.MFoe;

import static org.junit.Assert.*;

public class WallTerrainCase {

	@Test
	public void testWallBoxTerrain() {
		Region region = new SimpleRegionBuilder(this.getClass().getSimpleName())
				.withRoomPositions("1")
				.withRoom(1)
						.terrain("#########################")
						.terrain("#########################")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##      S              ##")
						.terrain("#########################")
						.terrain("#########################")
						.terrain("#########################")
						.build()
				.withStartPoint('S')
				.build();
		System.out.println(region);
		assertEquals(1, region.rooms.length);

		MegaManProfile p1 = new MegaManProfile();

		LevelManagerForTest manager = LevelManagerForTest.builder()
				.withPlayer1(p1)
				.build();
		manager.applyWorld(region);
		manager.runUntil(ignored -> manager.state == LevelWorld.STATE_PLAYING);

		final MFoe player1 = manager.player1;
		assertNotNull(player1);

		TestFoe testFoe = new TestFoe(player1.world);
		player1.world.createFoe(testFoe, 11.5f, 3, true);
		testFoe.flush();
		assertEquals(11, testFoe.pos.left(), 0);
		assertEquals(3, testFoe.pos.bottom(), 0);

		SimpleWorldInput input = new SimpleWorldInput();
		p1.setInput(input);
		input.setDown(WorldInput.MAP_RIGHT);
		manager.runUntil(ignored -> player1.pos.right() > 10);
		assertTrue(player1.pos.right() < testFoe.pos.left());
		manager.everyTick(ignored ->
				System.out.printf("%s (%f,%f)  rightTouched=%s%n",
						player1, player1.anchorX, player1.anchorY, player1.rightTouched));

		System.out.println("现在 testFoe 在 player1 右边." +
				"因为 testFoe 设置为 left wall 盒子地形, testFoe 是向右方向 (正方向), player1 应该过不到右边");

		long startTime = manager.ticker.worldTime;
		manager.runUntil(world -> world.ticker.worldTime > startTime + 1000);
		assertEquals(testFoe.pos.left(), player1.pos.right(), 0);
		assertTrue(player1.rightTouched);
	}

	static class TestFoe extends MFoe {
		public TestFoe(LevelWorld world) {
			super(world);
			this.type = TYPE_FOE;
			this.side = SIDE_ENEMY;

			this.createNormalPattern()
					.withRectangle(-0.5f, 0, 1, 1)
					.apply();

			terrainHandler = world.defaultHandler;
			setTerrainWithSide(BOX_TERRAIN_WALL_LEFT, SIDE_PLAYER);
		}

		@Override
		public String foeName() {
			return "TestFoe";
		}
	}
}
